/**
 *Main Class 
 *
 * version 1.0
 *
 * @author Jin Kit Chua
 * @version 20130319 V1.0
 */
package ch.zbw.iCoach.kit;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import ch.zbw.iCoach.remo.ActiveUser;

public class ICoachScreen {

	private JFrame mainFrame;

	private JButton help;
	private JButton Check;
	private JButton Close;

	private ICoachScreen ic;
	private ActiveUser au;
	private Languages la;

	private JPanel radioPanel1;
	private JPanel radioPanel2;
	private JPanel langPanel;
	private JPanel qPanel;
	private JPanel selectionPanel;
	private JPanel questionPanel;
	private JPanel buttonPanel;
	private JPanel smallQuestPanel;
	private JPanel smallAnsPanel;
	private JPanel keyPanel;
	private JPanel smallSelectPanel;
	private JPanel buttonPanel1;

	private JLabel question;
	private JLabel langQuest;
	private JLabel langAns;
	private JLabel message;
	private JLabel totalQuest;
	private JLabel totalCorrect;
	private JLabel totalWrong;

	private JRadioButton ans1;
	private JRadioButton ans2;
	private JRadioButton ans3;

	private JRadioButton qlang1;
	private JRadioButton qlang2;
	private JRadioButton qlang3;
	private JRadioButton qlang4;
	private JRadioButton alang1;
	private JRadioButton alang2;
	private JRadioButton alang3;
	private JRadioButton alang4;

	private int stringQuestion = 1;
	private int stringAnswer = 2;
	private int roundCounter = 0;
	private int correctCounter = 0;
	private int falseCounter = 0;
	private int correct = 0;
	private int roundNr = 1;
	private int totalCounter = 0;
	private int totalRightCounter = 0;
	private int totalWrongCounter = 0;

	private String part1;
	private String part2;
	private String part3;
	private String part4;

	/**
	 * 
	 * @param au reference for the active user
	 * @param la reference for the translation
	 */
	public ICoachScreen(ActiveUser au, Languages la) {

		this.ic = this;
		this.au = au;
		this.la = la;

		this.mainFrame = new JFrame("iCoach");

		this.Check = new JButton();
		this.Close = new JButton();
		this.help = new JButton();

		this.radioPanel1 = new JPanel();
		this.radioPanel2 = new JPanel();
		this.langPanel = new JPanel();
		this.qPanel = new JPanel();
		this.selectionPanel = new JPanel();
		this.questionPanel = new JPanel();
		this.buttonPanel = new JPanel();
		this.smallQuestPanel = new JPanel();
		this.smallAnsPanel = new JPanel();
		this.keyPanel = new JPanel();
		this.smallSelectPanel = new JPanel();
		this.buttonPanel1 = new JPanel();

		this.question = new JLabel("");
		this.langQuest = new JLabel();
		this.langAns = new JLabel();
		this.message = new JLabel();
		this.totalQuest = new JLabel();
		this.totalCorrect = new JLabel();
		this.totalWrong = new JLabel();

		this.ans1 = new JRadioButton();
		this.ans2 = new JRadioButton();
		this.ans3 = new JRadioButton();

		this.qlang1 = new JRadioButton("DE");
		this.qlang2 = new JRadioButton("EN");
		this.qlang3 = new JRadioButton("FR");
		this.qlang4 = new JRadioButton("IT");
		this.alang1 = new JRadioButton("DE");
		this.alang2 = new JRadioButton("EN");
		this.alang3 = new JRadioButton("FR");
		this.alang4 = new JRadioButton("IT");

		this.qlang1.setSelected(true);
		this.alang1.setEnabled(false);
		this.alang2.setSelected(true);
		this.qlang2.setEnabled(false);

		// Group the radio buttons so only one is selected
		ButtonGroup group1 = new ButtonGroup();
		group1.add(this.qlang1);
		group1.add(this.qlang2);
		group1.add(this.qlang3);
		group1.add(this.qlang4);

		ButtonGroup group2 = new ButtonGroup();
		group2.add(this.alang1);
		group2.add(this.alang2);
		group2.add(this.alang3);
		group2.add(this.alang4);

		ButtonGroup group3 = new ButtonGroup();
		group3.add(this.ans1);
		group3.add(this.ans2);
		group3.add(this.ans3);

		this.au.setNewRound(roundNr);//starting the round number 1
		this.roundCounter = au.getRoundSize();//getting the amount of question for the round
		this.generateQuestion();//first question for initializing
		this.paint();
	}

	public void paint() {
		this.mainFrame.setBackground(Color.lightGray);
		this.mainFrame.setResizable(false);

		this.updateLang(); //calls the method to update the language

		this.smallQuestPanel.setLayout(new GridLayout(3, 1, 20, 10));
		this.smallAnsPanel.setLayout(new GridLayout(3, 1, 20, 10));
		this.radioPanel1.setLayout(new GridLayout(1, 4, 20, 20));
		this.radioPanel2.setLayout(new GridLayout(1, 4, 20, 20));
		this.selectionPanel.setLayout(new GridLayout(3, 1, 10, 20));
		this.questionPanel.setLayout(new BorderLayout(10, 20));
		this.smallSelectPanel.setLayout(new BorderLayout(10, 20));

		this.mainFrame.setLayout(new GridLayout(1, 2, 20, 20));
		this.buttonPanel.setLayout(new GridLayout(1, 2, 20, 20));
		this.keyPanel.setLayout(new BorderLayout(10, 20));
		this.langPanel.setLayout(new GridLayout(3, 1, 20, 20));
		this.qPanel.setLayout(new GridLayout(3, 1, 20, 20));
		this.buttonPanel1.setLayout(new GridLayout(4, 1, 20, 20));

		this.buttonPanel.add(this.Close);
		// this.buttonPanel.add(this.help);

		this.keyPanel.add(this.buttonPanel, BorderLayout.SOUTH);
		this.keyPanel.add(this.message, BorderLayout.NORTH);
		this.buttonPanel1.add(this.totalQuest);
		this.buttonPanel1.add(this.totalCorrect);
		this.buttonPanel1.add(this.totalWrong);
		this.buttonPanel1.add(this.Check);

		this.radioPanel1.add(this.qlang1);
		this.radioPanel1.add(this.qlang2);
		this.radioPanel1.add(this.qlang3);
		this.radioPanel1.add(this.qlang4);
		this.radioPanel2.add(this.alang1);
		this.radioPanel2.add(this.alang2);
		this.radioPanel2.add(this.alang3);
		this.radioPanel2.add(this.alang4);

		this.selectionPanel.add(this.ans1);
		this.selectionPanel.add(this.ans2);
		this.selectionPanel.add(this.ans3);

		this.smallSelectPanel.add(this.selectionPanel, BorderLayout.NORTH);

		this.smallQuestPanel.add(this.langQuest, BorderLayout.NORTH);
		this.smallQuestPanel.add(this.radioPanel1, BorderLayout.CENTER);
		this.smallAnsPanel.add(this.langAns, BorderLayout.NORTH);
		this.smallAnsPanel.add(this.radioPanel2, BorderLayout.CENTER);

		this.questionPanel.add(this.question, BorderLayout.NORTH);

		this.qPanel.add(this.questionPanel);
		this.qPanel.add(this.smallSelectPanel);
		this.qPanel.add(this.buttonPanel1);

		this.langPanel.add(this.smallQuestPanel);
		this.langPanel.add(this.smallAnsPanel);
		this.langPanel.add(this.keyPanel);

		// this.mainFrame.add(this.buttonPanel, BorderLayout.SOUTH);
		this.mainFrame.add(this.qPanel);
		this.mainFrame.add(this.langPanel);
		this.mainFrame.setSize(800, 500);
		this.mainFrame.setVisible(true);
		this.mainFrame.setLocation(450, 250);

		this.Check.addActionListener(new ActionListenerCheck());
		this.Close.addActionListener(new ActionListenerClose());

		this.qlang1.addActionListener(new OpListenerGroup1());
		this.qlang2.addActionListener(new OpListenerGroup1());
		this.qlang3.addActionListener(new OpListenerGroup1());
		this.qlang4.addActionListener(new OpListenerGroup1());

		this.alang1.addActionListener(new OpListenerGroup2());
		this.alang2.addActionListener(new OpListenerGroup2());
		this.alang3.addActionListener(new OpListenerGroup2());
		this.alang4.addActionListener(new OpListenerGroup2());
	}

	// not allowing the user to use the same two languages in the trainer.
	class OpListenerGroup1 implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("DE")) {
				stringQuestion = 1;
				alang1.setEnabled(false);
				alang2.setEnabled(true);
				alang3.setEnabled(true);
				alang4.setEnabled(true);
			}
			if (e.getActionCommand().equals("EN")) {
				stringQuestion = 2;
				alang1.setEnabled(true);
				alang2.setEnabled(false);
				alang3.setEnabled(true);
				alang4.setEnabled(true);
			}
			if (e.getActionCommand().equals("FR")) {

				stringQuestion = 3;
				alang1.setEnabled(true);
				alang2.setEnabled(true);
				alang3.setEnabled(false);
				alang4.setEnabled(true);
			}
			if (e.getActionCommand().equals("IT")) {
				stringQuestion = 4;
				alang1.setEnabled(true);
				alang2.setEnabled(true);
				alang3.setEnabled(true);
				alang4.setEnabled(false);
			}
			System.out.println("StringQuestion:  " + stringQuestion);
		}
	}

	// not allowing the user to use the same two languages in the trainer.
	class OpListenerGroup2 implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("DE")) {
				stringAnswer = 1;
				qlang1.setEnabled(false);
				qlang2.setEnabled(true);
				qlang3.setEnabled(true);
				qlang4.setEnabled(true);
			}
			if (e.getActionCommand().equals("EN")) {
				stringAnswer = 2;
				qlang1.setEnabled(true);
				qlang2.setEnabled(false);
				qlang3.setEnabled(true);
				qlang4.setEnabled(true);
			}
			if (e.getActionCommand().equals("FR")) {

				stringAnswer = 3;
				qlang1.setEnabled(true);
				qlang2.setEnabled(true);
				qlang3.setEnabled(false);
				qlang4.setEnabled(true);
			}
			if (e.getActionCommand().equals("IT")) {
				stringAnswer = 4;
				qlang1.setEnabled(true);
				qlang2.setEnabled(true);
				qlang3.setEnabled(true);
				qlang4.setEnabled(false);
			}
			System.out.println("StringAnswer:  " + stringAnswer);
		}
	}

	//method to get and generate a new question
	public void generateQuestion() {
		this.totalCounter++;//increasing the total generated question counter
		String tempExercise = this.au.generateQuestion(this.stringQuestion,
				this.stringAnswer, this.correctCounter);

		String[] parts = tempExercise.split(","); //splitting the string into 4 words
		this.part1 = parts[0];//question word
		this.part2 = parts[1];//correct answer word
		this.part3 = parts[2];//random word
		this.part4 = parts[3];//random word

		this.question.setText(" " + this.part1);//displaying the questionword

		Random r;//randomizing 
		int randomNumber = 0;
		int nullOffset = 0;
		int upLimit = 3;//max number for random
		r = new Random();
		randomNumber = r.nextInt(upLimit) + nullOffset;

		if (randomNumber == 0) {//if 0 then set the answers in this order
			this.ans1.setText(" " + this.part2);
			this.ans2.setText(" " + this.part3);
			this.ans3.setText(" " + this.part4);
			this.correct = 1;
		}
		if (randomNumber == 1) {//if 1 then set the answers in this order
			this.ans1.setText(" " + this.part3);
			this.ans2.setText(" " + this.part2);
			this.ans3.setText(" " + this.part4);
			this.correct = 2;
		}
		if (randomNumber == 2) {//if 2 then set the answers in this order
			this.ans1.setText(" " + this.part4);
			this.ans2.setText(" " + this.part3);
			this.ans3.setText(" " + this.part2);
			this.correct = 3;
		}
	}

	// closing the icoachscreen
	public void close() {
		mainFrame.dispose();
	}

	//check if selection is correct or wrong
	class ActionListenerCheck implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			updateLang();
			boolean good = false;
			if (ans1.isSelected() && correct == 1) {//correct answer if the correct is 1
				String x;
				x = la.switchLangCorrect();//getting the message correct answer
				message.setText(x);
				good = true;
				au.rightAnswer(stringQuestion, stringAnswer, part1, part2);//leveling up the word
				generateQuestion();//get new question
				roundCounter--;//minus the counter to get to the new round
				totalRightCounter++;//add total counter
			}
			if (ans2.isSelected() && correct == 2) {//correct answer if the correct is 2
				String x;
				x = la.switchLangCorrect();
				message.setText(x);
				good = true;
				au.rightAnswer(stringQuestion, stringAnswer, part1, part2);
				generateQuestion();
				roundCounter--;
				totalRightCounter++;
			}
			if (ans3.isSelected() && correct == 3) {//correct answer if the correct is 3
				String x;
				x = la.switchLangCorrect();
				message.setText(x);
				good = true;
				au.rightAnswer(stringQuestion, stringAnswer, part1, part2);
				generateQuestion();
				roundCounter--;
				totalRightCounter++;
			}
			if (good == false) {//if answer is wrond
				String x;
				x = la.switchLangWrong();//getting the wrong message
				message.setText(x);
				falseCounter++;//counter of wrong answered
				totalWrongCounter++;
			}
			if (falseCounter == 2) {//only allowing the user to make 2 mistakes per question
				au.wrongAnswer(stringQuestion, stringAnswer, part1, part2);
				falseCounter = 0;
				generateQuestion();
				roundCounter--;
			}
			if (roundCounter == 0) {//if round is finished a new round starts
				roundNr++;
				au.setNewRound(roundNr);//leveling one round up after round completed
				roundCounter = au.getRoundSize();
			}
		}
	}

	class ActionListenerClose implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			new CloseScreen(ic, la);
		}
	}

	//updating the languages for each selected language
	public void updateLang() {
		String tempLang = this.la.switchLangICoach();
		String[] parts = tempLang.split(",");
		String langQ = parts[0];
		String langA = parts[1];
		String msg = parts[2];
		String buttonCheck = parts[3];
		String buttonClose = parts[4];
		String buttonHelp = parts[5];
		String total = parts[6];
		String correct = parts[7];
		String wrong = parts[8];

		this.langQuest.setText(langQ);
		this.langAns.setText(langA);
		this.message.setText(msg);
		this.Check.setText(buttonCheck);
		this.Close.setText(buttonClose);
		this.help.setText(buttonHelp);
		this.totalQuest.setText(total + "  " + this.totalCounter);//adding the totalcounter for reference
		this.totalCorrect.setText(correct + "  " + this.totalRightCounter);//adding the rightcounter for reference
		this.totalWrong.setText(wrong + "  " + this.totalWrongCounter);//adding the wrongcounter 

		this.ans1.setSelected(false);
		this.ans2.setSelected(false);
		this.ans3.setSelected(false);
	}
}
